//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Swap.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Instruments::CSwap::CSwap (Cephei::Core::IMatrix<Cephei::QL::ICashFlow^>^ legs, Cephei::Core::IVector<Boolean>^ payer, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CSwap::typeid)
{
    try
    {
#ifdef HANDLE
        _phSwap = NULL;
#endif
        legs ->Lock ();
        INativeMatrix<Cephei::QL::ICashFlow^>^ _NCIlegs = legs->getFeature (NativeFeature::shared_ptr);
        CCashFlowMatrix^ _NClegs = safe_cast<CCashFlowMatrix^>(_NCIlegs);
        std::vector<std::vector<boost::shared_ptr<QuantLib::CashFlow> >>& _legs = static_cast<std::vector<std::vector<boost::shared_ptr<QuantLib::CashFlow> >>&> (_NClegs->GetShared ());
        payer->Lock();
        INativeVector<Boolean>^ _NCIpayer = payer->getFeature (NativeFeature::Value);
        CBooleanVector^ _NCpayer = safe_cast<CBooleanVector^>(_NCIpayer);
        std::vector<bool>& _payer = static_cast<std::vector<bool>&> (_NCpayer->GetReference ());
        _ppSwap = new boost::shared_ptr<QuantLib::Swap> (new QuantLib::Swap ( _legs,  _payer ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (legs != nullptr) legs->Unlock();    //not optional
        if (payer != nullptr) payer->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::CSwap::CSwap (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ firstLeg, Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ secondLeg, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CSwap::typeid)
{
    try
    {
#ifdef HANDLE
        _phSwap = NULL;
#endif
        firstLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIfirstLeg = firstLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCfirstLeg = safe_cast<CCashFlowVector^>(_NCIfirstLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _firstLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCfirstLeg->GetShared ());
        secondLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIsecondLeg = secondLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCsecondLeg = safe_cast<CCashFlowVector^>(_NCIsecondLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _secondLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCsecondLeg->GetShared ());
        _ppSwap = new boost::shared_ptr<QuantLib::Swap> (new QuantLib::Swap ( _firstLeg,  _secondLeg ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (firstLeg != nullptr) firstLeg->Unlock();    //not optional
        if (secondLeg != nullptr) secondLeg->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::CSwap::CSwap (boost::shared_ptr<QuantLib::Swap>& childNative, Object^ owner) : CInstrument(CSwap::typeid)
{
#ifdef HANDLE
	_phSwap = NULL;
#endif
	_ppSwap = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
}
Cephei::QL::Instruments::CSwap::CSwap (QuantLib::Swap& childNative, Object^ owner) : CInstrument(CSwap::typeid)
{
#ifdef HANDLE
	_phSwap = NULL;
#endif
	_ppSwap = new boost::shared_ptr<QuantLib::Swap> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
    _SwapOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CSwap::CSwap (CSwap^ copy) : CInstrument(CSwap::typeid)
{
#ifdef HANDLE
	_phSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwap = new boost::shared_ptr<QuantLib::Swap> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
    }
}
Cephei::QL::Instruments::CSwap::CSwap (PLATFORM::Type^ t) : CInstrument(CSwap::typeid)
{
#ifdef HANDLE
	_phSwap = NULL;
#endif
	if (!t->IsSubclassOf(CSwap::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CSwap::CSwap (QuantLib::Handle<QuantLib::Swap>& childNative, Object^ owner)  : CInstrument(CSwap::typeid)
{
	_phSwap = &childNative;
	_ppSwap = &static_cast<boost::shared_ptr<QuantLib::Swap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
    _SwapOwner = owner;
}
Cephei::QL::Instruments::CSwap::CSwap (QuantLib::Handle<QuantLib::Swap> childNative)  : CInstrument(CSwap::typeid)
{
	_phSwap = &childNative;
	_ppSwap = &static_cast<boost::shared_ptr<QuantLib::Swap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CSwap::CSwap (QuantLib::Swap childNative)  : CInstrument(CSwap::typeid)
{
#ifdef HANDLE
	_phSwap = NULL;
#endif
	_ppSwap = new boost::shared_ptr<QuantLib::Swap> (new QuantLib::Swap (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSwap));
}
#endif

Cephei::QL::Instruments::CSwap::~CSwap ()
{
    if (_ppSwap != NULL)
    {
	    delete _ppSwap;
        _ppSwap = NULL;
    }
}
Cephei::QL::Instruments::CSwap::!CSwap ()
{
    if (_ppSwap != NULL)
    {
	    delete _ppSwap;
    }
}
QuantLib::Swap& Cephei::QL::Instruments::CSwap::GetReference ()
{
    if (_ppSwap == NULL) throw REFNEW NativeNullException ();
	return **_ppSwap;
}
boost::shared_ptr<QuantLib::Swap>& Cephei::QL::Instruments::CSwap::GetShared ()
{
    if (_ppSwap == NULL) throw REFNEW NativeNullException ();
	return *_ppSwap;
}
QuantLib::Swap* Cephei::QL::Instruments::CSwap::GetPointer ()
{
    if (_ppSwap == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Swap>& Cephei::QL::Instruments::CSwap::GetHandle ()
{
	if (_phSwap == NULL)
	{
		_phSwap = new Handle<QuantLib::Swap> (*_ppSwap);
	}
	return *_phSwap;
}
#endif
bool Cephei::QL::Instruments::CSwap::HasNative () 
{
	return (_ppSwap != NULL);
}

Boolean Cephei::QL::Instruments::CSwap::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSwap)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CSwap::Leg (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppSwap)->leg ( _j );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSwap::LegBPS (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwap)->legBPS ( _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSwap::LegNPV (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwap)->legNPV ( _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CSwap::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwap)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CSwap::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwap)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSwap::EndDiscounts (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppSwap)->endDiscounts ( _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSwap::NpvDateDiscount::get ()
{
    try
    {
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppSwap)->npvDateDiscount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CSwap::StartDiscounts (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppSwap)->startDiscounts ( _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ISwap^ Cephei::QL::Instruments::CSwap_Factory::Create (Cephei::Core::IMatrix<Cephei::QL::ICashFlow^>^ legs, Cephei::Core::IVector<Boolean>^ payer, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CSwap ( legs,  payer,  QL_Pricer);
}
Cephei::QL::Instruments::ISwap^ Cephei::QL::Instruments::CSwap_Factory::Create (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ firstLeg, Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ secondLeg, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CSwap ( firstLeg,  secondLeg,  QL_Pricer);
}
